package elcs.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import elcs.board.model.ListVO;
import elcs.group.model.GroupService;
import elcs.member.model.MemberService;
import elcs.member.model.MemberVO;
import elcs.point.model.TradePointService;

@Controller
public class ManageController {
	@Resource(name="memberServiceImpl")
	private MemberService memberService;
	
	@Resource(name="groupServiceImpl")
	private GroupService groupService;
	@Resource(name="tradePointServiceImpl")
	private TradePointService tradePointService;
	
	
	@RequestMapping("/manage/manage.do")
	public String manageView(MemberVO mvo)
	{
		System.out.println("manageView");
		
		return "main.manage";
	}
	
	
	//12월 13일 수정부분

	@RequestMapping("/manage/memberManage.do")
	public ModelAndView memberView(HttpServletRequest request) throws Exception
	{
		System.out.println("memberView");
		ListVO lvo=memberService.findAllMember(request);
		System.out.println(lvo);
		return new ModelAndView ("member.manage","lvo",lvo);
	}
	@RequestMapping("/manage/groupManage.do")
	public ModelAndView groupView(HttpServletRequest request)
	{
		System.out.println("groupView");
		ListVO lvo=groupService.findAllGroup(request);
		System.out.println(lvo);
		return new ModelAndView ("group.manage","lvo",lvo);
		
	}
	
	
	
	@RequestMapping("/manage/memberInfoById.do")
	public ModelAndView memberInfoById(String id)
	{
		System.out.println("memberInfoById");
		MemberVO mvo =memberService.findByMemberId(id);
	
		
		return new ModelAndView ("memberview.manage","memvo",mvo);
	}
	
	@RequestMapping("/manage/memberUpdate.do")
	public ModelAndView memberUpdate(MemberVO mvo,int beforePoint)
	{
		System.out.println("memberUpdate "+ beforePoint);
		System.out.println(mvo);
		memberService.updateMemberByLevelPoint(mvo);
		if(mvo.getPoint()>beforePoint)
		{
			tradePointService.plusPointByName(mvo.getId(),"운영자",mvo.getPoint()-beforePoint);
			
		}
		else if(mvo.getPoint()<beforePoint)
		{
			tradePointService.minusPointByName(mvo.getId(), "운영자", beforePoint-mvo.getPoint());
		}
		System.out.println(mvo);
		
		return new ModelAndView ("redirect:/manage/memberInfoById.do?id="+mvo.getId());
	}
	@RequestMapping("/manage/suchmemberManage.do")
	public ModelAndView memberView(HttpServletRequest request,String method,String value) throws Exception
	{
		System.out.println("memberView");
		ListVO lvo=memberService.manageFindByMethodAllList(request,method,value);
		System.out.println(lvo);
		return new ModelAndView ("suchmember.manage","lvo",lvo);
	}
	
	@RequestMapping("manage/groupInfoByName.do")
	public ModelAndView groupInfoByName(String name) 
	{
		System.out.println("groupInfoByName  "+name);
		Map map=groupService.findGroupByName(name);
		System.out.println(map);
		return new ModelAndView ("groupview.manage","map",map);
	}
	
	
	
	
	
	
	
}
